package com.liyuan.help_e.controller;

import com.liyuan.help_e.model.Page;
import com.liyuan.help_e.model.Question;
import com.liyuan.help_e.model.ResultJson;
import com.liyuan.help_e.model.User;
import com.liyuan.help_e.service.PageService;
import com.liyuan.help_e.service.QuestionService;
import com.liyuan.help_e.service.ReplyService;
import com.liyuan.help_e.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PersonalController {
    @Autowired
    UserService userService;
    @Autowired
    QuestionService questionService;
    @Autowired
    PageService pageService;
    @Autowired
    ReplyService replyService;
    @GetMapping("/personal/questions/{page}")
    public ResultJson personalQuestions(@PathVariable("page")Integer page){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userService.selectUserByUsername(username);
        Question[] questions = questionService.selectPersonalQuestionsByUserId(user.getId(),page,8);
        return new ResultJson(200,"get perosnal questions success!",questions);

    }

    @GetMapping("/personal/questions/page/{currentPage}")
    public ResultJson getPagination_questions(@PathVariable("currentPage")Integer currentPage){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userService.selectUserByUsername(username);
        Page page = pageService.pagingByQuestionsUserId(currentPage,8,user.getId());
        return new ResultJson(200,"paging success ->currentPage:"+currentPage,page);
    }
    @GetMapping("/personal/replies/{page}")
    public <T> ResultJson personal_replies(@PathVariable("page")Integer page){
        return new ResultJson(200,"get message success!",replyService.dealReplies());
    }

    @GetMapping("/personal/replies/page/{currentPage}")
    public ResultJson getPagination_replies(@PathVariable("currentPage")Integer currentPage){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userService.selectUserByUsername(username);
        Page page = pageService.paging(currentPage,8,replyService.sum(user.getId()));
        return new ResultJson(200,"paging success ->currentPage:"+currentPage,page);
    }
}
